package previous.Test_0307;

import java.util.Stack;

/**
 * Created with Intellij IDEA
 * Description:
 * User: 星辰
 */
public class Solution {
    //删除字符串中所有相邻字符串的重复项
    public String removeDuplicates1(String s) {
          char[] c=s.toCharArray();
        Stack<Character> stack=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if(stack.empty()){
                stack.push(c[i]);
            }else{
                if(stack.peek()==c[i]) stack.pop();
                else stack.push(c[i]);
            }
        }
        StringBuilder stringBuilder=new StringBuilder();
        for (Character x:stack) {
             stringBuilder.append(x);
        }
        return stringBuilder.toString();
    }
    public String removeDuplicates(String s) {
          StringBuilder stringBuilder=new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if(stringBuilder.length()==0){
                stringBuilder.append(s.charAt(i));
            }else{
                if(stringBuilder.charAt(stringBuilder.length()-1)==s.charAt(i)){
                    stringBuilder.deleteCharAt(stringBuilder.length()-1);
                }else{
                    stringBuilder.append(s.charAt(i));
                }
            }
        }
        return stringBuilder.toString();
        }
    //比较含退格的字符串
    public boolean backspaceCompare(String s, String t) {
       String ret1=back(s);
        String ret2=back(t);
        return ret1.equals(ret2);
    }
    public String back(String s){
        StringBuilder s1=new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if(s1.length()==0 && s.charAt(i)!='#'){
                s1.append(s.charAt(i));
            }else{
                if(s.charAt(i)!='#') s1.append(s.charAt(i));
                else if(s1.length()>0)s1.deleteCharAt(s1.length()-1);
            }

        }
        return s1.toString();
    }
    public boolean backspaceCompare1(String s, String t) {
        StringBuilder s1=new StringBuilder();
        StringBuilder t1=new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if(s1.length()==0 && s.charAt(i)!='#'){
                s1.append(s.charAt(i));
            }else{
                if(s.charAt(i)!='#') s1.append(s.charAt(i));
                else if(s1.length()>0)s1.deleteCharAt(s1.length()-1);
            }

        }
        for (int i = 0; i < t.length(); i++) {
            if(t1.length()==0 && t.charAt(i)!='#'){
                t1.append(t.charAt(i));
            }else{
                if(t.charAt(i)!='#') t1.append(t.charAt(i));
                else if(t1.length()>0)t1.deleteCharAt(t1.length()-1);
            }
        }
        return s1.toString().equals(t1.toString());
    }
    //基本计数器
    public int calculate(String s) {
          int len=s.length();
          Stack<Integer> stack=new Stack<>();
          int i=0;
          int n=s.length();
          char op='+';
          while(i<n){
              if(s.charAt(i)==' ') i++;
              //注意是字符
              else if(s.charAt(i)>='0' && s.charAt(i)<='9'){
                  //一个或者多个数字
                  int tmp=0;
                  while(i<n && s.charAt(i)>='0' && s.charAt(i)<='9'){
                      tmp=tmp*10+(s.charAt(i)-'0');
                      i++;
                  }
                  switch (op){
                      case '+': stack.push(tmp); break;
                      case '-': stack.push(-tmp); break;
                      case '/': stack.push(stack.pop()/tmp); break;
                      case '*': stack.push(stack.pop()*tmp); break;
                  }
              }else{
                  op=s.charAt(i);
                  i++;
              }
          }
          int ret=0;
        for (Integer x:stack) {
            ret+=x;
        }
        return ret;
    }
}
